package com.mentalhealthplatform.global.rbac.dto.normal;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.mentalhealthplatform.global.rbac.model.ButtonComponent;
import lombok.Getter;
import lombok.Setter;

import java.util.Set;
import java.util.stream.Collectors;

@Getter
@Setter
public class ButtonComponentDTO {
    private Long id;
    private String identifier;
    private String displayName;
    private String displayType;
    private String description;
    private Integer orderNum;
    private boolean visible;
    private boolean status;
    private Long parentId;
    private Set<Long> endpointIds;

    @JsonIgnore
    private Set<SimpleEndpointDTO> endpoints;

    @JsonProperty("endpoints")
    public Set<SimpleEndpointDTO> getEndpoints() {
        return endpoints;
    }

    public ButtonComponentDTO(ButtonComponent buttonComponent) {
        this.id = buttonComponent.getId();
        this.identifier = buttonComponent.getIdentifier();
        this.displayName = buttonComponent.getDisplayName();
        this.displayType = buttonComponent.getDisplayType();
        this.description = buttonComponent.getDescription();
        this.orderNum = buttonComponent.getOrderNum();
        this.visible = buttonComponent.isVisible();
        this.status = buttonComponent.isStatus();

        if (buttonComponent.getParentMenu() != null){
            this.parentId = buttonComponent.getParentMenu().getId();
        }


        // 检查 endpoints 是否为 null
        if (buttonComponent.getEndpoints() != null) {
            this.endpoints = buttonComponent.getEndpoints().stream()
                    .map(endpoint -> new SimpleEndpointDTO(endpoint))
                    .collect(Collectors.toSet());
        } else {
            this.endpoints = null;
        }
    }

    public ButtonComponentDTO(){ }



}
